package com.sth.test2;

import com.sth.test1.Processor1;
import lombok.SneakyThrows;
import org.apache.commons.lang.StringUtils;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

/**
 * @Author: sitianhong
 * @Description:
 * @Since: create in 2021/11/30 0030 8:47
 */
public class MyJComponent2 {
    public static JComponent getTest2Pannel() {
        JPanel panel = new JPanel(false);
        panel.setLayout(null);    //面板使用网格4X4布局
        panel.setBounds(400, 400, 400, 400);
        JLabel label = new JLabel("选择文件夹：");
        label.setBackground(Color.red);
        label.setBounds(50, 10, 120, 100);
        JLabel label2 = new JLabel("文件命名：");
        label2.setBounds(50, 40, 120, 100);
        JLabel label3 = new JLabel("处理情况展示：");
        label3.setBounds(50, 70, 120, 100);
        JTextField jtf = new JTextField(25);
        jtf.setBounds(150, 50, 450, 25);
        JTextField jtf2 = new JTextField(25);// 处理后的文件名称
        jtf2.setBounds(150, 80, 450, 25);
        JTextArea jtf3 = new JTextArea();
        jtf3.setBounds(150, 115, 560, 120);
        JButton button = new JButton("浏览");
        button.setBounds(605, 50, 100, 25);
        panel.add(label);
        panel.add(jtf);
        panel.add(button);
        panel.add(label2);
        panel.add(jtf2);
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                JFileChooser fc = new JFileChooser("F:\\");
                fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
                int val = fc.showOpenDialog(null);    //文件打开对话框
                if (val == fc.APPROVE_OPTION) {
                    //正常选择文件
                    jtf.setText(fc.getSelectedFile().toString());
                } else {
                    //未正常选择文件，如选择取消按钮
                    jtf.setText("请选择文件");
                }
            }
        });
        JButton button2 = new JButton("开始处理");
        button2.setBounds(605, 80, 100, 25);
        panel.add(button2);
        panel.add(label3);
        panel.add(jtf3);
        button2.addActionListener(new ActionListener() {
            @SneakyThrows
            public void actionPerformed(ActionEvent arg0) {
                if (StringUtils.isNotBlank(jtf.getText()) && StringUtils.isNotBlank(jtf2.getText())) {
                    // 给参数进行处理
                    Processor2.getFilePath(jtf.getText(), jtf2.getText());
                    jtf3.setText("文件 : " + jtf2.getText() + "已生成" + " 在测试文件目录下");
                } else {
                    JOptionPane.showMessageDialog(panel, "未选择文件或命名文件!", "提示", 1);
                }

            }
        });
        return panel;
    }
}
